package com.yq.tanhua.ui.chat.conversationlist

import TanHuaTheme
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.fmt.mvi.learn.gobal.log
import com.yq.tanhua.im.ImUtils
import com.yq.tanhua.ui.chat.chatlist.ChatListActivity
import com.yq.tanhua.ui.contract.ContractActivity
import dagger.hilt.android.AndroidEntryPoint
import net.lucode.hackware.magicindicator.buildins.UIUtil

@AndroidEntryPoint
class ConversationFragment : Fragment() {
    val conversationViewMode: ConversationViewMode by viewModels()

    @SuppressLint("StateFlowValueCalledInComposition")
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = ComposeView(inflater.context).apply {
        layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )

        val data = conversationViewMode.conversationViewUIData
        "ConversationFragment ComposeView".log()

        setContent {
            TanHuaTheme {
                Column {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .background(Color.Red)
                            .padding(top = 34.dp, bottom = 10.dp)
                    ) {
                        Text(
                            text = "消息",
                            modifier = Modifier.align(Alignment.Center),
                            style = TextStyle(fontSize = 20.sp, color = Color.White)
                        )
                        Text(
                            text = "联系人",
                            modifier = Modifier
                                .align(Alignment.CenterEnd)
                                .clickable {
                                    val intent = Intent(activity, ContractActivity::class.java)
                                    startActivity(intent)
                                },
                            style = TextStyle(fontSize = 14.sp, color = Color.White)
                        )
                    }
                    Spacer(modifier = Modifier.height(20.dp))
                    ConversationListView(conversationViewUIData = data, onClick = { conversation ->
                        ChatListActivity.actionStart(
                            context,
                            conversation.conversationId(),
                            ImUtils.getChatType(conversation)
                        )
                    })
                }
            }

        }
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        conversationViewMode.getConversionListData()
    }

}